package com.dl.mood.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.dl.mood.R;

import com.dl.mood.databinding.LayoutMoodListItemBinding;
import com.dl.mood.model.MoodItem;

import java.util.List;

public class MoodListAdapter extends RecyclerView.Adapter<MoodListAdapter.MoodListViewHolder> {
    private static final String TAG = "MoodListAdapter";
    List<MoodItem> moodItems;

    public MoodListAdapter() {
    }

    public void setMoodItems(List<MoodItem> moodItems) {
        this.moodItems = moodItems;
        notifyDataSetChanged();
    }

    public MoodListAdapter(List<MoodItem> moodItems) {
        this.moodItems = moodItems;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MoodListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutMoodListItemBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.layout_mood_list_item, parent, false
        );
        return new MoodListViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MoodListViewHolder holder, int position) {
        MoodItem moodItem = moodItems.get(position);

        holder.binding.imageView2.setImageResource(moodItem.getResId());
        holder.binding.title.setText(moodItem.getTitle());
        holder.binding.content.setText(moodItem.getContent());
        holder.binding.sysTime.setText(moodItem.getSysTime());
        holder.binding.executePendingBindings();

    }

    @Override
    public int getItemCount() {
        return moodItems != null ? moodItems.size() : 0;
    }


    static class MoodListViewHolder extends RecyclerView.ViewHolder {
        private final LayoutMoodListItemBinding binding;

        public MoodListViewHolder(@NonNull LayoutMoodListItemBinding binding) {

            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
